package com.ibigcat.module.mdm.service.impl;

import com.ibigcat.module.mdm.service.CodeRuleService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.ibigcat.module.mdm.controller.admin.coderule.vo.*;
import com.ibigcat.module.mdm.dao.entity.CodeRuleDO;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.util.object.BeanUtils;

import com.ibigcat.module.mdm.dao.mapper.CodeRuleMapper;

import static com.ibigcat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ibigcat.module.mdm.enums.ErrorCodeConstants.*;

/**
 * 编码规则配置 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CodeRuleServiceImpl implements CodeRuleService {

    @Resource
    private CodeRuleMapper codeRuleMapper;

    @Override
    public Long createCodeRule(CodeRuleSaveReqVO createReqVO) {
        // 插入
        CodeRuleDO codeRule = BeanUtils.toBean(createReqVO, CodeRuleDO.class);
        codeRuleMapper.insert(codeRule);
        // 返回
        return codeRule.getId();
    }

    @Override
    public void updateCodeRule(CodeRuleSaveReqVO updateReqVO) {
        // 校验存在
        validateCodeRuleExists(updateReqVO.getId());
        // 更新
        CodeRuleDO updateObj = BeanUtils.toBean(updateReqVO, CodeRuleDO.class);
        codeRuleMapper.updateById(updateObj);
    }

    @Override
    public void deleteCodeRule(Long id) {
        // 校验存在
        validateCodeRuleExists(id);
        // 删除
        codeRuleMapper.deleteById(id);
    }

    private void validateCodeRuleExists(Long id) {
        if (codeRuleMapper.selectById(id) == null) {
            throw exception(CODE_RULE_NOT_EXISTS);
        }
    }

    @Override
    public CodeRuleDO getCodeRule(Long id) {
        return codeRuleMapper.selectById(id);
    }

    @Override
    public PageResult<CodeRuleDO> getCodeRulePage(CodeRulePageReqVO pageReqVO) {
        return codeRuleMapper.selectPage(pageReqVO);
    }

}